package com.hg.library.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SoundEffectConstants;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * Created by ASUS-PC on 2016/6/1.
 */
public class CuteLayoutView extends RelativeLayout implements CuteImView {

    protected Context mContext;

    protected CuteView leftView;

    protected CuteView rightView;

    protected CuteView topView;

    protected CuteView bottomView;

    protected float translateX;

    protected float translateY;

    protected Bitmap bitmap;

    private ImageView bg;

    public CuteLayoutView(Context context) {
        super(context);
        initView(context);
    }

    public CuteLayoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);
    }

    private void initView(Context context) {
        mContext = context;
        bg = new ImageView(mContext);
        LayoutParams layoutParams = new LayoutParams(-1,-2);
        bg.setLayoutParams(layoutParams);
        addView(bg);
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    private void initView(Context context, AttributeSet attrs) {
        mContext = context;
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    @Override
    public void setImage(Bitmap bitmap) {
        setImage(bitmap, 0);
    }

    @Override
    public void setImage(int round) {
        if (bitmap != null) {
            setImage(bitmap, round);
        } else {
            throw new NullPointerException("bitmap == null");
        }
    }

    @Override
    public void setImage(Bitmap bitmap, int round) {
        this.bitmap = bitmap;
        Bitmap bitmap1 = toRoundCorner(this.bitmap, round);
        bg.setImageBitmap(bitmap1 != null ? bitmap1 : bitmap);
    }

    @Override
    public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
        playSoundEffect(SoundEffectConstants.CLICK);
        return super.requestFocus(direction, previouslyFocusedRect);
    }

    @Override
    public Bitmap getImage() {
        return bitmap;
    }

    @Override
    public CuteView getLeftView() {
        return leftView;
    }

    @Override
    public CuteView getRightView() {
        return rightView;
    }

    @Override
    public CuteView getTopView() {
        return topView;
    }

    @Override
    public CuteView getBottomView() {
        return bottomView;
    }

    @Override
    public void setLeftView(CuteView leftView) {
        this.leftView = leftView;
    }

    @Override
    public void setRightView(CuteView rightView) {
        this.rightView = rightView;
    }

    @Override
    public void setTopView(CuteView topView) {
        this.topView = topView;
    }

    @Override
    public void setBottomView(CuteView bottomView) {
        this.bottomView = bottomView;
    }

    public float getTranslateX() {
        return translateX;
    }

    public void setTranslateX(float translateX) {
        this.translateX = translateX;
    }

    public float getTranslateY() {
        return translateY;
    }

    public void setTranslateY(float translateY) {
        this.translateY = translateY;
    }

    @Override
    public View getView() {
        return this;
    }

    protected Bitmap toRoundCorner(Bitmap bitmap, float round) {
        return null;
    }

}
